package com.fzrskj.cloud.free.miniservice.service;

import com.alibaba.fastjson.JSONObject;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.miniservice.service.vo.MiniServiceTokenBean;
import com.fzrskj.cloud.free.miniservice.utils.MiniServiceTokenUtils;
import com.fzrskj.cloud.free.sys.bean.SysMiniServiceBean;
import com.fzrskj.cloud.free.sys.utils.SysMiniServiceUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * MiniServicePublicService.
 *
 * @author zj.
 *         Created on 2019/5/23 0023.
 */
public class MiniServicePublicService extends CObject {
    /**
     * 微服务：生成管理Token.
     */
    public String mimiServicePublicManageToken (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        String serviceCode = JsonTool.getStrValue(p, "serviceCode", "");
        if (StrTool.isBlankStr(serviceCode)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }
        MiniServiceTokenUtils tokenUtils = new MiniServiceTokenUtils();
        MiniServiceTokenBean tokenBean = tokenUtils.createToken(serviceCode);
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JSONObject.toJSONString(tokenBean));
    }

    public String mimiServicePublicUserToken (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        String serviceCode = JsonTool.getStrValue(p, "serviceCode", "");
        if (StrTool.isBlankStr(serviceCode)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }
        Integer userId = CloudContextUtils.getSession().getUID();
        MiniServiceTokenUtils tokenUtils = new MiniServiceTokenUtils();
        MiniServiceTokenBean tokenBean = tokenUtils.createToken(serviceCode,userId);
        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JSONObject.toJSONString(tokenBean));
    }

    /**
     * 微服务：验证管理Token.
     */
    public String  mimiServicePublicManageCheck (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        String token = JsonTool.getStrValue(p, "token", "");
        Integer refresh = JsonTool.getIntValue(p, "refresh", 0);
        if (StrTool.isBlankStr(token)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        MiniServiceTokenUtils tokenUtils = new MiniServiceTokenUtils();
        MiniServiceTokenBean tokenBean = tokenUtils.getToken(token, refresh);
        if (null== tokenBean) {
            return XmlCodeParser.formatResultTips(false, "TOKEN ERROR", "");
        }

        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JSONObject.toJSONString(tokenBean));
    }

    /**
     * 微服务：获取配置.
     */
    public String mimiServicePublicInfo (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        String serviceCode = JsonTool.getStrValue(p, "serviceCode", "");

        if (StrTool.isBlankStr(serviceCode)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        SysMiniServiceUtils serviceUtils = new SysMiniServiceUtils();
        SysMiniServiceBean sysMiniServiceBean = serviceUtils.getByCode(serviceCode);

        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JSONObject.toJSONString(sysMiniServiceBean));
    }
}
